package com.example.multidrive;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.multidrive.R.id;

public class MainActivity extends Activity {

	DatabaseSQL multidriveDb = null;
	SQLiteDatabase db = null;
	private String disciplina;

	ArrayAdapter<String> disciplinasAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getLista();
	}

	public void getLista() {
		ArrayList<String> disciplinas = new ArrayList<String>();
		disciplinasAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, disciplinas);
		ListView listViewDisciplinas = (ListView) findViewById(id.listViewDisciplinas);
		listViewDisciplinas.setAdapter(disciplinasAdapter);

		listViewDisciplinas.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> disciplinasAdapter, View v,
					int position, long id) {

				disciplina = disciplinasAdapter.getItemAtPosition(position)
						.toString();

				new AlertDialog.Builder(MainActivity.this)
						.setItems(
								new String[] { "Ver Notas",
										"Remover Disciplina" },
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										Intent intent = null;
										if (which == 0) {
											intent = new Intent(
													getApplicationContext(),
													Notas.class);

											intent.putExtra("nomeDisciplina",
													disciplina);
											startActivity(intent);

										} else if (which == 1) {
											db = multidriveDb
													.getReadableDatabase();
											db.execSQL("DELETE FROM disciplinas WHERE nome = '"
													+ disciplina + "'");
											db.close();
											multidriveDb.close();
											getLista();

											return;
										} else {
											return;
										}

									}
								}).setPositiveButton("Cancelar", null).show();
			}
		});

		try {
			multidriveDb = new DatabaseSQL(getApplicationContext());
			atualizarLista();
		} catch (Exception e) {
			new AlertDialog.Builder(this).setMessage(e.getMessage())
					.setPositiveButton("OK", null).show();
		}
	}

	protected void atualizarLista() {
		db = multidriveDb.getReadableDatabase();

		Cursor cursor = db.rawQuery("SELECT * FROM disciplinas", null);
		cursor.moveToFirst();

		while (!cursor.isAfterLast()) {
			String nome = cursor.getString(1);
			disciplinasAdapter.add(nome);
			cursor.moveToNext();
		}
		cursor.close();
		db.close();
		multidriveDb.close();
	}

	public void adicionarDisciplina(View view) {
		try {
			EditText txtDisciplina = (EditText) findViewById(id.editTextDisciplina);
			String disciplina = txtDisciplina.getText().toString();
			db = multidriveDb.getWritableDatabase();

			if (!txtDisciplina.getText().toString().equals("")) {
				db.execSQL("INSERT INTO disciplinas (nome) VALUES ('"
						+ disciplina + "')");
				db.close();
				multidriveDb.close();
				txtDisciplina.setText("");

				disciplinasAdapter.add(disciplina);
			} else {
				new AlertDialog.Builder(this)
						.setMessage("PREENCHA O CAMPO AO LADO")
						.setPositiveButton("OK", null).show();
			}

		} catch (Exception ex) {
			new AlertDialog.Builder(this).setMessage(ex.getMessage())
					.setPositiveButton("OK", null).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
